package com.dkt.android.mvp.contract

import android.app.Activity
import com.dkt.android.core.AppManager
import com.dkt.android.core.RouterHub
import com.dkt.android.mvp.model.BackUserInfo
import com.dkt.android.mvp.model.UserInfo
import com.dkt.android.utils.Utils
import com.dtb.utils.commons.utils.BeanUtils
import com.dtb.utils.commons.utils.PreTools


/**
 * @author: dtb
 * @email: wendaoyuqin1@gmail.com
 * @createTime: 19-3-12下午8:35
 */
object UserContractImpl : UserContract {

    private val userInfo = UserInfo()

    private val pres: PreTools = PreTools(AppManager.instance, "consumer")

    override fun updateUserDate(userInfo: BackUserInfo?): Boolean {
        if (BeanUtils.mergeObject(this.userInfo, userInfo)) {
            saveSelf()
            return true
        }
        return false
    }

    override fun saveSelf() {
        BeanUtils.putObjectInPre(userInfo, pres)
    }


    override fun loginSuccess(userInfo: BackUserInfo?, psw: String): Boolean {
        return if (userInfo != null) {
            updateUserDate(userInfo)
        } else {
            false
        }
    }

    override fun loginOut(activity: Activity?) {
        this.userInfo.token = ""
        saveSelf()
        Utils.navigation(RouterHub.APP_LOGIN_ACTIVITY)
        activity?.finish()
    }

    override fun isUserLogin(): Boolean {
        return !userInfo.token.isNullOrBlank()
    }

    override fun getToken(): String {
        return userInfo.token ?: ""
    }

    override fun getUserData(): UserInfo {
        return userInfo
    }

    override fun initUser() {
        BeanUtils.getObjectPre(userInfo, pres)
    }

}